package aufgabenserie_1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

public class Analyser {
	
	public static void main(String[] args){
		analyse(new File("gutenberg.txt"));
	}
	
	public static void analyse(File input){
		int letterCounter = 0;
		TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
		
		try {
			FileReader fr = new FileReader(input);
			
			int charCount = 0;
			while(fr.ready()){
				char ch = (char) fr.read();
				ch = Character.toLowerCase(ch);
				if(ch != ' '){					
					if(tm.containsKey(ch)){
						tm.put(ch, tm.get(ch)+1);						
					}else{
						tm.put(ch, 1);
					}
					letterCounter++;
				}
			}
			
			System.out.println("Anzahl unterschiedlicher Zeichen: "+tm.size());
			System.out.println("Anzahl Zeichen Gesamt: "+letterCounter);
			
			ArrayList<CharAmount> ca = new ArrayList<CharAmount>();
			for (Character elem : tm.keySet()) {
				ca.add( new CharAmount(tm.get(elem), elem, letterCounter));
			}
			
			java.util.Collections.sort(ca);
			
			for (CharAmount elem : ca) {
				System.out.println(elem);
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
